#ifndef bitset_h
#define bitset_h

#include <iostream>
#include <vector>
using namespace std;

namespace yfy
{
template <size_t N>
class bitset
{
public:
    bitset()
    {
        _bits.resize(N / 8 + 1, 0);
    }
    void set(size_t x)
    {
        size_t i = x / 8;   //计算出这个数在第几个char上
        size_t j = x % 8;   //计算出这个数在这个char中的第几个比特位上
        _bits[i] |= (1 << j);
    }
    void reset(size_t x)
    {
        size_t i = x / 8;   //计算出这个数在第几个char上
        size_t j = x % 8;   //计算出这个数在这个char中的第几个比特位上
        _bits[i] &= (~(1 << j));
    }
    bool test(size_t x)
    {
        size_t i = x / 8;   //计算出这个数在第几个char上
        size_t j = x % 8;   //计算出这个数在这个char中的第几个比特位上
        return _bits[i] & (1 << j);
    }
private:
    vector<char> _bits;
};

void TestBitset()
{
    bitset<(unsigned int)-1> bs;
    bs.set(4);
    cout << bs.test(4) << endl;
    bs.reset(4);
    cout << bs.test(4) << endl;
}

template <size_t N>
class FindOnceValSet
{
public:
    void set(size_t x)
    {
        bool flag1 = _bs1.test(x);
        bool flag2 = _bs2.test(x);
        if(!flag1 && !flag2)
        {
            _bs2.set(x);
        }
        else if(!flag1 && flag2)
        {
            _bs1.set(x);
            _bs2.reset(x);
        }
    }
    
    void print_once_num()
    {
        for(size_t i = 0; i < N; i++)
        {
            if(_bs1.test(i) == false && _bs2.test(i) == true)
            {
                cout << i << " ";
            }
        }
        cout << endl;
    }
    
private:
    bitset<N> _bs1;
    bitset<N> _bs2;
};

void TestFindOnceValSet()
{
    int a[] = { 1, 20, 30, 43, 5, 4, 1, 43, 43, 7, 9, 7, 7, 0 };
    FindOnceValSet<100> fovs;
    for (auto e : a)
    {
        fovs.set(e);
    }

    fovs.print_once_num();
}

}

#endif /* bitset_h */
